// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var GLB = require("Glb");
var uiPanel = require("uiPanel");
cc.Class({
    extends: uiPanel,
    properties: {
        //小鸟节点
        birdParent:cc.Node,
        //水管节点
        pipeParent0:cc.Node,
        pipeParent1:cc.Node,
        pipeParent2:cc.Node,

        bg0:cc.Node,
        bg1:cc.Node,
        bg2:cc.Node,
        bg3:cc.Node,

        jumpClip: {
            default: null,
            url: cc.AudioClip
        },
    },

    onLoad() {
        this.inumber=0;
        this.Dead = false;
        Game.RoadManager = this;
        this._super();
        this.nodeDict["click"].on("click", this.jump, this);
    },
    start(){
        var pipeStertOffsetX = 1200;
        var spaceX = ((1500+135)/3);
        this.pipeParent0.x = pipeStertOffsetX + spaceX*0;
        this.pipeParent1.x = pipeStertOffsetX + spaceX*1;
        this.pipeParent2.x = pipeStertOffsetX + spaceX*2;
        cc.director.getScheduler().schedule(this.gameupdatetime, this, 0.01);
    },


    jump:function () {
        cc.audioEngine.play(this.jumpClip, false, 0.3);
        var msg = {
            action: GLB.PLAYER_STEP_DATA,
            rivalY:this.birdParent.y
        };
        Game.GameManager.sendEventEx(msg);
    },


    //碰撞檢測
    checkCollision(bird, pipe) {

        if(this.Dead){
            if(GLB.isRoomOwner&&Game.PlayerManager.rival.Dead){
                Game.PlayerManager.player.gameOver();
            }
            return;
        }
        if (bird.x + 51 < pipe.x - 64 && bird.y > -475) {
            return;
        }
        if (bird.x - 51 > pipe.x + 64 && bird.y > -475) {
            return;
        }
        if ((bird.y + 36 < pipe.y + 120) && (bird.y - 36 > pipe.y - 120)) {
            return;
        }
        this.Dead = true;
        if(Game.PlayerManager.rival.Dead){
            Game.PlayerManager.player.gameOver();
        }else {
            var msg={
                action:GLB.GAME_DEAD,
                playerID:this.playerId,
            }
            Game.PlayerManager.player.node.active=false;
            Game.GameManager.sendEvent(msg);
        }
    },
    //天空背景
    moveBg(bg){
        bg.x =bg.x - 3;
        if(bg.x<-750){
            bg.x =  bg.x + 750*2;
        }
    },

    //地面背景循環

    moveBgGround(bg){
        bg.x =bg.x - 3;
        if(bg.x<-750){
            bg.x = bg.x + 750*2 ;
        }
    },

    //水管循环
    movePipe(pipe){
        pipe.x = pipe.x - 6;
        if (pipe.x < (-750-78)){
            if(!this.Dead){
                Game.PlayerManager.player.selfScore+=1;
            }
            if(!Game.PlayerManager.rival.Dead){
                Game.PlayerManager.rival.rivalScore+=1;
            }
            if (GLB.isRoomOwner) {
                pipe.x=pipe.x +1500+135;
                var msg = {
                    action: GLB.ROAD_DATA,
                    playerId: this.playerId,
                    random:Math.random()*350,
                    pipeX:pipe.x
                };
                Game.GameManager.sendEventEx(msg);
            }
        }
    },
    movePipeUp:function(random,pipeX){
        if(this.inumber==0){
            this.pipeParent0.y = 150-parseFloat(random);
            this.pipeParent0.x = parseFloat(pipeX);
            this.inumber++;
        }else if(this.inumber==1){
            this.pipeParent1.y = 150-parseFloat(random);
            this.pipeParent1.x = parseFloat(pipeX);
            this.inumber++;
        }else if(this.inumber==2){
            this.pipeParent2.y = 150-parseFloat(random);
            this.pipeParent2.x = parseFloat(pipeX);
            this.inumber=0;
        }
    },
    update(dt) {

    },

    gameupdatetime:function () {
        if (Game.GameManager.gameState !== GameState.Play) {
            return;
        }
        this.moveBg(this.bg0);
        this.moveBg(this.bg1);

        this.moveBgGround(this.bg2);
        this.moveBgGround(this.bg3);

        this.movePipe(this.pipeParent0);
        this.movePipe(this.pipeParent1);
        this.movePipe(this.pipeParent2);

        this.checkCollision(this.birdParent,this.pipeParent0);
        this.checkCollision(this.birdParent,this.pipeParent1);
        this.checkCollision(this.birdParent,this.pipeParent2);
    }
});
